{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "8bW5uwdFfj5K",
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\SONAL BANSAL\\Anaconda3\\lib\\site-packages\\sklearn\\ensemble\\weight_boosting.py:29: DeprecationWarning: numpy.core.umath_tests is an internal NumPy module and should not be imported. It will be removed in a future NumPy release.\n",
      "  from numpy.core.umath_tests import inner1d\n"
     ]
    }
   ],
   "source": [
    "import random,math,copy,time\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.ensemble import (RandomForestClassifier,GradientBoostingClassifier)\n",
    "from sklearn import tree\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split,cross_val_score\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn import preprocessing, svm\n",
    "from tqdm import tqdm\n",
    "import time\n",
    "from sklearn.model_selection import GridSearchCV"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "S6-dFa8Jfj5Q"
   },
   "outputs": [],
   "source": [
    "m=20  # Problem dimension (number of decision variables\n",
    "Ap=0.1  # Awareness probability\n",
    "fl=2   # Flight length (fl)\n",
    "wf=1.2# wf is used to control the importance of classification accuracy and number of selected features.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "VSQ4rCRjfj5V"
   },
   "outputs": [],
   "source": [
    "df=pd.read_csv(\"AD-MCI-CN.csv\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "yBZV56q2m6-s"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>label</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "      <th>8</th>\n",
       "      <th>...</th>\n",
       "      <th>67590</th>\n",
       "      <th>67591</th>\n",
       "      <th>67592</th>\n",
       "      <th>67593</th>\n",
       "      <th>67594</th>\n",
       "      <th>67595</th>\n",
       "      <th>67596</th>\n",
       "      <th>67597</th>\n",
       "      <th>67598</th>\n",
       "      <th>67599</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>55</td>\n",
       "      <td>56</td>\n",
       "      <td>57</td>\n",
       "      <td>57</td>\n",
       "      <td>57</td>\n",
       "      <td>57</td>\n",
       "      <td>56</td>\n",
       "      <td>55</td>\n",
       "      <td>56</td>\n",
       "      <td>...</td>\n",
       "      <td>59</td>\n",
       "      <td>59</td>\n",
       "      <td>59</td>\n",
       "      <td>59</td>\n",
       "      <td>59</td>\n",
       "      <td>59</td>\n",
       "      <td>60</td>\n",
       "      <td>59</td>\n",
       "      <td>58</td>\n",
       "      <td>57</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>254</td>\n",
       "      <td>253</td>\n",
       "      <td>254</td>\n",
       "      <td>...</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>254</td>\n",
       "      <td>253</td>\n",
       "      <td>254</td>\n",
       "      <td>...</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>254</td>\n",
       "      <td>253</td>\n",
       "      <td>254</td>\n",
       "      <td>...</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>254</td>\n",
       "      <td>253</td>\n",
       "      <td>254</td>\n",
       "      <td>...</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 67601 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   label    0    1    2    3    4    5    6    7    8  ...    67590  67591  \\\n",
       "0      0   55   56   57   57   57   57   56   55   56  ...       59     59   \n",
       "1      0  255  255  255  255  255  255  254  253  254  ...      255    255   \n",
       "2      0  255  255  255  255  255  255  254  253  254  ...      255    255   \n",
       "3      0  255  255  255  255  255  255  254  253  254  ...      255    255   \n",
       "4      0  255  255  255  255  255  255  254  253  254  ...      255    255   \n",
       "\n",
       "   67592  67593  67594  67595  67596  67597  67598  67599  \n",
       "0     59     59     59     59     60     59     58     57  \n",
       "1    255    255    255    255    255    255    255    255  \n",
       "2    255    255    255    255    255    255    255    255  \n",
       "3    255    255    255    255    255    255    255    255  \n",
       "4    255    255    255    255    255    255    255    255  \n",
       "\n",
       "[5 rows x 67601 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = df.dropna(how='all')\n",
    "# df=df.drop(columns=['Unnamed: 0'])\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "PPwseQsIglXa"
   },
   "outputs": [],
   "source": [
    "val_Data = pd.read_csv(\"AD-MCI-CN_val.csv\")\n",
    "val_Data = val_Data.dropna(how='all')\n",
    "#val_Data=val_Data.drop(columns=['Unnamed: 0'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 235
    },
    "colab_type": "code",
    "id": "wmWJFkWDgu_7",
    "outputId": "c147cdca-b994-4f9b-8b49-22c8ca7d71e1"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>label</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "      <th>8</th>\n",
       "      <th>...</th>\n",
       "      <th>67590</th>\n",
       "      <th>67591</th>\n",
       "      <th>67592</th>\n",
       "      <th>67593</th>\n",
       "      <th>67594</th>\n",
       "      <th>67595</th>\n",
       "      <th>67596</th>\n",
       "      <th>67597</th>\n",
       "      <th>67598</th>\n",
       "      <th>67599</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>254</td>\n",
       "      <td>253</td>\n",
       "      <td>254</td>\n",
       "      <td>...</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>254</td>\n",
       "      <td>253</td>\n",
       "      <td>254</td>\n",
       "      <td>...</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>254</td>\n",
       "      <td>253</td>\n",
       "      <td>254</td>\n",
       "      <td>...</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>254</td>\n",
       "      <td>253</td>\n",
       "      <td>254</td>\n",
       "      <td>...</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>254</td>\n",
       "      <td>253</td>\n",
       "      <td>254</td>\n",
       "      <td>...</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "      <td>255</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 67601 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   label    0    1    2    3    4    5    6    7    8  ...    67590  67591  \\\n",
       "0      0  255  255  255  255  255  255  254  253  254  ...      255    255   \n",
       "1      0  255  255  255  255  255  255  254  253  254  ...      255    255   \n",
       "2      0  255  255  255  255  255  255  254  253  254  ...      255    255   \n",
       "3      0  255  255  255  255  255  255  254  253  254  ...      255    255   \n",
       "4      0  255  255  255  255  255  255  254  253  254  ...      255    255   \n",
       "\n",
       "   67592  67593  67594  67595  67596  67597  67598  67599  \n",
       "0    255    255    255    255    255    255    255    255  \n",
       "1    255    255    255    255    255    255    255    255  \n",
       "2    255    255    255    255    255    255    255    255  \n",
       "3    255    255    255    255    255    255    255    255  \n",
       "4    255    255    255    255    255    255    255    255  \n",
       "\n",
       "[5 rows x 67601 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val_Data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "_W4D-XnDfj5j"
   },
   "outputs": [],
   "source": [
    "features = [i for i in range(0,67600)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "VvAFiZhvfj5n"
   },
   "outputs": [],
   "source": [
    "d=67600# Flock (population) size\n",
    "lt=67600\n",
    "y_train=df['label']\n",
    "x_train=df[df.columns[1:]]\n",
    "x_train.columns=features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "K-e_XSa4pKr8"
   },
   "outputs": [],
   "source": [
    "y_test = val_Data['label']\n",
    "x_test = val_Data[val_Data.columns[1:]]\n",
    "x_test.columns=features"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## WITH GRID-SEARCH AND WITHOUT BIO-INSPIRED"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**RFF**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# from sklearn.model_selection import RandomizedSearchCV\n",
    "rforest1 = RandomForestClassifier()\n",
    "rfparams = {'n_estimators':[10,100,200,300,400,500,600], 'criterion':['gini','entropy'], 'max_depth':[None,10,20,30,50], 'min_samples_split':[2,5,10], 'min_samples_leaf':[1,2,4],'bootstrap':[True, False]}\n",
    "\n",
    "grid_rf = GridSearchCV(estimator = rforest1, param_grid= rfparams,  cv = 3, verbose=2,  n_jobs = -1)\n",
    "\n",
    "\n",
    "grid_rf.fit(x_train, y_train)\n",
    "\n",
    "# print(grid_rf.grid_scores_)\n",
    "print(grid_rf.best_score_)\n",
    "print(grid_rf.best_params_)\n",
    "\n",
    "\n",
    "\n",
    "grid_rf.predict(x_test)\n",
    "gridacc_rf=grid_rf.score(x_test,y_test)\n",
    "print(\"Accuracy\",gridacc_rf)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**DT**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dtree1 = tree.DecisionTreeClassifier()\n",
    "# criterion=’gini’, splitter=’best’, max_depth=None, min_samples_split=2, min_samples_leaf=1,\n",
    "dtparams = {'criterion':['gini', 'entropy'], 'splitter':['best','random'],  'max_depth':[None,10,20,30,50], 'min_samples_split':[2,5,10], 'min_samples_leaf':[1,2,4]}\n",
    "grid_dt = GridSearchCV(dtree1, dtparams, verbose=2)\n",
    "\n",
    "\n",
    "grid_dt.fit(x_train, y_train)\n",
    "\n",
    "\n",
    "print(grid_dt.best_score_)\n",
    "print(grid_dt.best_params_)\n",
    "# print(grid_dt.grid_scores_)\n",
    "\n",
    "grid_dt.predict(x_test)\n",
    "grid_dtacc=grid_dt.score(x_test,y_test)\n",
    "print(grid_dtacc)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**KNN**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neigh1 = KNeighborsClassifier()\n",
    "\n",
    "knnparams = {'algorithm':['auto', 'ball_tree', 'kd_tree', 'brute'], 'n_neighbors':[3, 4, 5, 6, 7], 'leaf_size':[10, 20, 30, 40], 'p':[1, 2, 3]}\n",
    "grid_knn = GridSearchCV(neigh1, knnparams, verbose=2)\n",
    "grid_knn.fit(x_train, y_train)\n",
    "grid_knn.predict(x_test)\n",
    "print(grid_knn.best_score_)\n",
    "print(grid_knn.best_params_)\n",
    "\n",
    "grid_neighacc=grid_knn.score(x_test,y_test)\n",
    "print(grid_neighacc)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**SVM**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "clf1 = svm.SVC()\n",
    "\n",
    "svcparams = {'C':[1.0,10,100,1000], 'kernel':['rbf','linear', 'poly', 'sigmoid'], 'degree':[3,4,5], 'gamma':['auto',0.1,0.01,0.001]}\n",
    "grid_svc = GridSearchCV(clf1, svcparams, verbose=2)\n",
    "grid_svc.fit(x_train, y_train)\n",
    "grid_svc.predict(x_test)\n",
    "print(grid_svc.best_score_)\n",
    "print(grid_svc.best_params_)\n",
    "\n",
    "grid_clfacc=grid_svc.score(x_test,y_test)\n",
    "print(grid_clfacc)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f=open('AD-MCI-CN_parameters.txt','a')\n",
    "\n",
    "txt1=str(grid_rf.best_params_)\n",
    "f.write('RFF='+txt1+'\\n')\n",
    "\n",
    "txt2=str(grid_dt.best_params_)\n",
    "f.write('DT='+txt2+'\\n')\n",
    "\n",
    "txt3=str(grid_knn.best_params_)\n",
    "f.write('KNN='+txt3+'\\n')\n",
    "\n",
    "txt4=str(grid_svc.best_params_)\n",
    "f.write('SVM='+txt4+'\\n')\n",
    "\n",
    "f.flush()\n",
    "f.close()"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "collapsed_sections": [],
   "name": "myccsa.ipynb",
   "provenance": [],
   "version": "0.3.2"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
